<template>
	<view class="login">
		<view class="content">
			<image src="@/static/logo.png"></image>
			<up-checkbox-group @change="handleChange">
				<up-checkbox v-model="checked" activeColor="#f1692b" label="同意用户协议与隐私条款" :name="true">
				</up-checkbox>
			</up-checkbox-group>
			<view class="text">
				—————————— 一键快速注册/登录 ——————————
			</view>
			<!-- <view class="button" @click="handleLogin">微信授权注册/登录</view> -->
			<button class="button" open-type="getPhoneNumber" @getphonenumber="getphonenumber">微信授权注册/登录</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import utils from '@/utils/util.js'
	import login from '@/api/login.js'
	const code = ref('')
	const nickName = ref('')
	const avatarUrl = ref('')
	const encryptedData = ref('')
	const iv = ref('')
	const signature = ref('')
	const checked = ref([])
	onMounted(() => {
		uni.login({
			provider: 'weixin', //使用微信登录
			success: function(loginRes) {
				console.log(loginRes);
				code.value = loginRes.code
				// uni.getUserInfo({
				// 	provider: 'weixin',
				// 	success(infoRes) {
				// 		console.log(infoRes);
				// 		nickName.value = infoRes.userInfo.nickName
				// 		avatarUrl.value = infoRes.userInfo.avatarUrl
				// 		encryptedData.value = infoRes.encryptedData
				// 		iv.value = infoRes.iv
				// 		signature.value = infoRes.signature
				// 	}
				// })
			}
		});
	})
	const handleChange = (e) => {
		checked.value = e
	}
	const getphonenumber = (e) => {
		console.log(e);
		if(e.detail.code) {
			handleLogin(e.detail.code)
		}
	}
	const handleLogin = async (phone_code) => {
		if (!checked.value[0]) {
			utils.toast('请同意用户协议')
			return
		}
		const {
			status,
			info,
			data
		} = await login.wechatAuthLogin({
			code: code.value,
			phone_code: phone_code
			// name: nickName.value,
			// avatar: avatarUrl.value,
			// encryptedData: encryptedData.value,
			// iv: iv.value,
			// signature: signature.value
		})
		utils.toast(info)
		if (status == 1) {
			uni.setStorageSync('token', data.token)
			uni.switchTab({
				url: '/pages/home/index'
			})
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		display: flex;
		align-items: center;
		height: 100vh;
		justify-content: center;

		.content {
			margin: 0 auto;
			width: 90%;
			text-align: center;

			image {
				width: 190rpx;
				height: 190rpx;
				border-radius: 20rpx;
				margin-bottom: 200rpx;
			}

			.text {
				font-size: 20rpx;
			}

			.button {
				margin-top: 100rpx;
				background: #f1692b;
				border-radius: 100rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 600;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 38rpx;
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				height: 100rpx;
			}

			::v-deep .u-checkbox-group {
				justify-content: center;
				margin: 40rpx 0;
			}
		}
	}
</style>